Developer.com Click here to support our advertisers
Click here to support our advertisers
SOFTWARE FOR SALE
BOOKS FOR SALE
SEARCH CENTRAL
*JOB BANK
*CLASSIFIEDS
*DIRECTORIES
*REFERENCE
Online Library
*LEARNING CENTER
*JOURNAL
*NEWS CENTRAL
*DOWNLOADS
*COMMUNITY
*CALENDAR
*ABOUT US
-----
Journal:

Get the weekly email highlights from the most popular journal for developers!
Current issue -----
developer.com
developerdirect.com
htmlgoodies.com
javagoodies.com
jars.com
intranetjournal.com
javascripts.com

REFERENCE

All Categories : C/C++

vclp06.htm



- Project 6 -
Looping Back and Forth


In this lesson, you learned about Visual C++'s looping capabilities. You saw the following:

  • Loops repeat sections of code.

  • The while loop tests a condition at the top of the loop and loops as long as the condition is true.

  • Loops and the if statement differ because the body of an if executes one time at most.

  • The do-while loop tests its condition at the bottom of the loop, ensuring that the loop always executes at least once.

  • The break statement terminates the current loop.

  • The for loop gives you more control when you know in advance the number of iterations required by the loop.

  • When you nest loops, the inner loop iterates faster than the outer loop.

Project 6 Listing. Using a for loop to compute interest controlled by the user's input.


  1:// Filename: PROJECT6.CPP

  2:// Computes interest over a number of periods

  3:#include <iostream.h>

  4:

  5:

  6:void main()

  7:{

  8:  int periods;       // Number of periods in the loan

  9:  int count;         // Loop control variable

 10:  float intRate;     // Interest rate per period

 11:  float principal;   // Loan amount

 12:

 13:  cout << "Welcome to loan central!" << endl;   // Title

 14:  cout << "------------------------" << endl << endl;

 15:

 16:  cout << "How much was the loan for? ";

 17:  cin >> principal;

 18:

 19:  cout << endl << "What is the interest rate (i.e., .03 ";

 20:  cout << "for 3%) per period? ";

 21:  cin >> intRate;

 22:

 23:  cout << endl << "How many periods are in the loan? ";

 24:  cin >> periods;

 25:

 26:  for (count = 0; count < periods; count++)

 27:    { principal *= (1 + intRate); }   // Compounds the interest

 28:

 29:  cout.precision(2);

 30:  cout.setf(ios::showpoint);

 31:  cout.setf(ios::fixed);

 32:  cout << endl << "$" << principal << " total amount owed after "

 33:       << periods << " periods." << endl;

 34:  return;

 35:}

Output

      
      Welcome to loan central!
      
      ------------------------
      
      How much was the loan for? 2500.00
      
      What is the interest rate (i.e., .03 for 3%) per period? .11
      
      How many periods are in the loan? 5
      
      $4212.65 total amount owed after 5 periods.

Description

1: A C++ comment that includes the program's filename.

2: A C++ comment that contains the program's description.

3: cout and cin need information in the IOSTREAM.H header file.

4: Place blank lines throughout a program for readability.

5: Place blank lines throughout a program for readability.

6: All functions have names, and the first function in all C++ programs is main().

7: All functions begin with a left brace.

8: Defines an integer variable that will hold the number of periods in the loan.

9: Defines an integer variable that will control the for loop.

10: Defines a floating-point variable that will hold the interest rate.

11: Defines a floating-point variable that will hold the loan principal (the amount borrowed).

12: Extra blank lines make your program more readable.

13: Prints a title.

14: Underlines the title on-screen with hyphens.

15: Extra blank lines make your program more readable.

16: Prompts the user for the amount of the loan (the principal).

17: Gets the principal from the user.

18: Extra blank lines make your program more readable.

19: Prompts for the interest rate. The user is reminded to enter the interest as a decimal.

20: The rest of line 19's cout.

21: Gets the interest rate.

22: Extra blank lines make your program more readable.

23: Prompts for the number of periods in the loan (called the loan term).

24: Gets the term from the user.

25: Extra blank lines make your program more readable.

26: The for loop ensures that the interest calculation computes for the full term entered by the user in line 25.

27: The body of the for loop is only a single statement. The principal increases by the amount of each period's interest rate.

28: Extra blank lines make your program more readable.

29: Ensures that two decimal places print.

30: Prints the decimal point.

31: Guards against scientific notation.

32: Prints the computed loan principal including all accrued interest.

33: Continues the loan balance's printing.

34: Returns to QuickWin control window.

38: main()'s closing brace to terminate the program.



1: You saw another version of this program in Unit 7, but that program didn't have the looping capability that this program does. As a result, Unit 7's program was longer and less flexible!

9: All for loops require a loop control variable.

26: The for loop compounds the loan in one step.


Ruler image
Contact reference@earthweb.com with questions or comments.
Copyright 1998 EarthWeb Inc., All rights reserved.
PLEASE READ THE ACCEPTABLE USAGE STATEMENT.
Copyright 1998 Macmillan Computer Publishing. All rights reserved.

Click here for more info

Click here for more info